package com.music.glide.load.model.stream

import com.music.glide.load.KOption
import com.music.glide.load.KOptions
import com.music.glide.load.data.KHttpUrlFetcher
import com.music.glide.load.model.KGlideUrl
import com.music.glide.load.model.KModelCache
import com.music.glide.load.model.KModelLoader
import java.io.InputStream

class KHttpGliderUrlLoader(private val modelCache: KModelCache<KGlideUrl, KGlideUrl>? = null) :
    KModelLoader<KGlideUrl, InputStream> {

    override fun buildLoadData(
        model: KGlideUrl, width: Int, height: Int, options: KOptions
    ): KModelLoader.LoadData<InputStream> {

        var url:KGlideUrl? = model
        if (modelCache != null) {
            url = modelCache.get(model, 0, 0)
            if (url == null){
                modelCache.put(model,0,0,model)
                url = model
            }
        }
        return KModelLoader.LoadData(
            url!!,
            fetcher = KHttpUrlFetcher(url, TIMEOUT.getDefaultValue()!!)
        )
    }

    override fun handles(model: KGlideUrl): Boolean {
        return true
    }

    companion object {
        val TIMEOUT= KOption.memory("HttpGlideUrlLoader.Timeout",25000)
    }

}